package com.aaa.tars.service.impl;

import com.aaa.tars.dao.TicketDao;
import com.aaa.tars.entity.Ticket;
import com.aaa.tars.service.TicketService;
import com.aaa.tars.util.CustomException;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @FileName: TicketServiceImpl
 * @Description: 门票服务实现类
 * @Author: 李宗盛
 * @CreateTime: 2025/9/12 11:14
 * @Version: 17
 */
@Service
public class TicketServiceImpl implements TicketService {
    @Resource
    private TicketDao ticketDao;

    @Override
    public List<Ticket> queryByScenicId(Integer scenicId) {
        if (scenicId == null || scenicId <= 0) {
            throw new CustomException(400, "景区ID无效");
        }
        // 直接查询景区下所有门票，无状态过滤（移除原有的ticket_status=1的条件）
        return ticketDao.queryByScenicId(scenicId);
    }

    // 新增：实现关联查询方法（查询门票+下属订单）
    @Override
    public Ticket queryTicketWithOrders(Integer ticketId) {
        if (ticketId == null || ticketId <= 0) {
            throw new CustomException(ResultStatus.ERROR.getStatusCode(), "门票ID非法");
        }
        // 复用queryById，因Mapper的TicketWithOrderAndScenicMap已配置关联查询
        return ticketDao.queryById(ticketId);
    }
    
    // 新增：实现关联查询方法（查询门票+景区，不关联订单）
    @Override
    public Ticket queryTicketWithScenicOnly(Integer ticketId) {
        if (ticketId == null || ticketId <= 0) {
            throw new CustomException(ResultStatus.ERROR.getStatusCode(), "门票ID非法");
        }
        return ticketDao.queryTicketWithScenicOnly(ticketId);
    }
    
    @Override
    public Ticket queryById(Integer ticketId) {
        if (ticketId == null || ticketId <= 0) { // 补充异常处理
            throw new CustomException(ResultStatus.ERROR.getStatusCode(), ResultStatus.ERROR.getStatusMessage());
        }
        return ticketDao.queryById(ticketId);
    }



    @Override
    public PageInfo queryByPage(Page<Ticket> page) {
        //判断pageNo和pageSize 不能为空
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageNo == 0 || pageSize == null || pageSize == 0) {
            throw new IllegalArgumentException("非法参数异常");
        }
        PageHelper.startPage(pageNo, pageSize);
        Ticket data = page.getData();
        return new PageInfo(ticketDao.queryAll(data));
    }

    @Override
    public int add(Ticket ticket) {
        // 设置创建时间和更新时间
        Date now = new Date();
        // ticket.setCreateTime(now);
        // ticket.setUpdateTime(now);
        return ticketDao.add(ticket);
    }

    @Override
    public int update(Ticket ticket) {
        // 设置更新时间
        // ticket.setUpdateTime(new Date());
        return ticketDao.update(ticket);
    }

    @Override
    public int deleteById(Integer ticketId) {
        return ticketDao.deleteById(ticketId);
    }
}